package aad.storyboard.controller;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import aad.storyboard.model.Animation;
import aad.storyboard.model.AnimationProvider;
import aad.storyboard.model.AnimationStateStarted;
import aad.storyboard.model.BlurEffect;
import aad.storyboard.model.Image;
import aad.storyboard.model.Layer;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Script;
import aad.storyboard.model.Story;
import android.util.Log;



public class SceneController extends Observable implements Observer{
	
	public static SceneController instance = new SceneController();
	
	private long currentTime = 0;
	private Story story;
	private String subtitles;	
	private FrameRateController frc;
	
	private SceneController(){
	}
	
	@Override
	public void update(Observable timer, Object data) {
		 currentTime=((StoryTimer) timer).getCurrentTime();
		 Scene currentScene = Story.getInstance().getCurrentChapter().getCurrentScene();
		 currentScene.setCurrentTime((int) currentTime);
		 		 
		 //PlannedAnimations
		 AnimationProvider acontroller = AnimationProvider.getInstance();		 
		 for(Animation a : acontroller.getPlannedAnimations()){
			 //Log.e("TAG","Checking animation "+a+" with time "+a.getTime()+" and active "+a.isActive()+" and currenttime "+currentTime);
			 if((a.getTime() < currentTime) && (!a.isStarted())){
				 frc.addObserver(a);				 
				 a.start();
				 AnimationProvider.getInstance().addAnimationToStack(a);
			 }
		 }	
		 
		 //TouchAnimations		 		 
		 Integer key = new Integer(AnimationProvider.EVENT_TOUCH);
		 ArrayList<Animation> touchAnimations = AnimationProvider.getInstance().getOnEventAnimations().get(key);
		 if (touchAnimations!=null){
		 for(Animation a : touchAnimations){
			if(touchAnimations != null){
			 if((a.getTime() < currentTime) && (!a.isStarted())){
				 frc.addObserver(a);
				 for (Image img:a.getImages())
					{
						BlurEffect blur=new BlurEffect(img);
						img.setBlurBitmap(blur.getAlphaBitmap());
						img.setblurPaint(blur.getBlurPaint());						
					}				 
				 AnimationProvider.getInstance().addAnimationToStack(a);
			 }
			}
		 }
		 }
		 
		//SceneTransitions
		 TransitionProvider trprovider=TransitionProvider.getInstance();
		 for (SceneTransition st: trprovider.getTransitions())
		 {	System.out.println("Scene transition check"+currentTime+" "+st.getTime()) ;
			 if (st.getTime()<currentTime&&!st.isStarted())
			 {	System.out.println("Scene transition found") ;
				 st.execute();
			 }
			 
		 }
		 
		 //Subtitles
		
		 /*for(int i=0; i<currentScene.getScripts().size(); i++){		        
	          if(currentTime>=currentScene.getScripts().get(i).getStartTime()&&!currentScene.getScripts().get(i).isStarted()){	       	   
	        	  currentScene.setCurrentScript(currentScene.getScripts().get(i));
	        	  currentScene.getScripts().get(i).setStarted(true);
	        	  System.out.println("Currenttime "+currentTime+"Scenetime: "+currentScene.getScripts().get(i).getStartTime()+currentScene.getScripts().get(i).getText());
	        	  break;
	          }
		 }*/
         
				 
	}

	public static SceneController getInstance(Story s,FrameRateController frc) {
		instance.setStory(s);
		instance.setFrameRateController(frc);
		return instance;
	}

	private void setStory(Story s) {
		this.story = s;
	}

	public void setSubtitles(String subtitles) {
		this.subtitles = subtitles;
	}

	public String getSubtitles() {
		return subtitles;
	}
	
	public void setFrameRateController(FrameRateController frc){
		this.frc = frc;
	}

}
